package com.ls.kdlss.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.gm.common.utils.ToastUtil;
import com.gm.lib.utils.UserInfoDB;
import com.ls.kdlss.ui.Login.LogActivity;
import com.ls.kdlss.ui.Login.LoginActivity;
import com.ls.kdlss.ui.MainActivity;
import com.wx.android.common.util.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

public class GuideUtil implements View.OnTouchListener {

    private static List<View> viewList;


    private static Context mContext;

    public static void onGuidel(final Context context, final ViewPager viewpager, @LayoutRes int resource[], Class mClass, @IdRes int rid, LayoutInflater getlayoutinflater) {
        View[] views = new View[resource.length];
        mContext = context;
        LayoutInflater inflater = getlayoutinflater;
        for (int i = 0; i < resource.length; i++) {
            views[i] = inflater.inflate(resource[i], null);
        }
        final MyAdapter myAdapter = new MyAdapter();

        if (isonelogin(context)) {
            viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
            for (int i = 0; i < resource.length; i++) {
                viewList.add(views[i]);
            }
            viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                //判断是不是要跳转下页一个标记位
                private boolean flag;

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {


                }

                @Override
                public void onPageScrollStateChanged(int state) {

                    switch (state) {
                        case ViewPager.SCROLL_STATE_DRAGGING:
                            //拖的时候才进入下一页
                            flag = false;
                            Log.d("vivi", "SCROLL_STATE_DRAGGING: " + ViewPager.SCROLL_STATE_DRAGGING);

                            break;
                        case ViewPager.SCROLL_STATE_SETTLING:
                            flag = true;
                            Log.d("vivi", "SCROLL_STATE_SETTLING: " + ViewPager.SCROLL_STATE_SETTLING);
                            break;
                        case ViewPager.SCROLL_STATE_IDLE:
                            Log.d("vivi", "SCROLL_STATE_IDLE: " + ViewPager.SCROLL_STATE_IDLE + "  mViewPager.getCurrentItem() " + viewpager.getCurrentItem());
                            /**
                             * 判断是不是最后一页，同是是不是拖的状态
                             */
                            if (viewpager.getCurrentItem() == myAdapter.getCount() - 1 && !flag) {
                            Intent localIntent = new Intent();

                                Intent intent = new Intent((Activity) context, LoginActivity.class);
                                context.startActivity(intent);
                                Activity thisactivity = (Activity) context;
                                thisactivity.finish();
                        }
                            flag = true;

                            break;
                    }

                }
            });

            Button button = (Button) views[views.length - 1].findViewById(rid);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    /*Intent intent = new Intent((Activity) context, MainActivity.class);
                    context.startActivity(intent);
                    Activity thisactivity = (Activity) context;
                    thisactivity.finish();*/
                }
            });

            viewpager.setAdapter(myAdapter);

        } else {
            ActivityUtil.startActivity(context, mClass);
            Activity thisactivity = (Activity) context;
            thisactivity.finish();
        }
    }


    //适配器
    private static class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    }


    private static Boolean isonelogin(Context context) {
        SharedPreferences sp = context.getSharedPreferences("DSXisone", Context.MODE_PRIVATE);
        String isone = sp.getString("isone", "no");
        if (isone.equals("no")) {
            sp.edit().putString("isone", "yes").commit();
            return true;
        } else {
            return false;

        }

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return false;
    }


}
